package com.shanshan.crowd.controller;

import com.shanshan.crowd.constant.CrowdConstant;
import com.shanshan.crowd.entity.po.MemberPO;
import com.shanshan.crowd.service.api.MemberService;
import com.shanshan.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description
 * @ClassName MemberProviderController
 * @Author 山山大魔王
 * @Date 2022/7/28 17:50
 * @Version 1.0
 */
@RestController
public class MemberProviderController {

    @Autowired
    private MemberService memberService;

    @RequestMapping("/save/member/remote")
    public ResultEntity<String> saveMember(@RequestBody MemberPO memberPO){
        try {
            memberService.saveMember(memberPO);

            return ResultEntity.successWithoutData();
        } catch (Exception exception) {

            if (exception instanceof DuplicateKeyException){
                return ResultEntity.failed(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
            }

            return ResultEntity.failed(exception.getMessage());
        }
    }

    @RequestMapping("/get/memberpo/by/login/acct/remote")
    public ResultEntity<MemberPO> getMemberPOByLoginAcctRemote(@RequestParam("loginacct") String loginacct){

        try {

            // 1.调用本地Service完成查询
            MemberPO memberPO = memberService.getMemberPOByLoginAcct(loginacct);

            // 2.如果没有异常就返回
            return ResultEntity.successWithData(memberPO);
        } catch (Exception exception) {
            exception.printStackTrace();

            // 3.如果捕获到异常就返回失败的结果
            return ResultEntity.failed(exception.getMessage());
        }

    }
}
